package com.haima.ziliao.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haima.common.utils.DateUtils;
import com.haima.common.utils.SecurityUtils;
import com.haima.ziliao.domain.*;
import com.haima.ziliao.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.haima.common.annotation.Log;
import com.haima.common.core.controller.BaseController;
import com.haima.common.core.domain.AjaxResult;
import com.haima.common.enums.BusinessType;
import com.haima.common.utils.poi.ExcelUtil;
import com.haima.common.core.page.TableDataInfo;

/**
 * 我的收藏Controller
 * 
 * @author hima
 * @date 2024-11-07
 */
@RestController
@RequestMapping("/ziliao/shoucang")
public class PShoucangController extends BaseController
{
    @Autowired
    private IPShoucangService pShoucangService;
    @Autowired
    private IPZiliaoService ziliaoService;
    @Autowired
    private IPDuwuService duwuService;
    @Autowired
    private IPChanpinService chanpinService;
    @Autowired
    private IPLvxingsheService lvxingsheService;

    /**
     * 查询我的收藏列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:shoucang:list')")
    @GetMapping("/list")
    public TableDataInfo list(PShoucang pShoucang)
    {
        startPage();
        List<PShoucang> list = pShoucangService.selectPShoucangList(pShoucang);
        return getDataTable(list);
    }

    @GetMapping("/getMyShoucang")
    public TableDataInfo getMyShoucang(PShoucang pShoucang)
    {
        pShoucang.setUid(SecurityUtils.getUserId());
        startPage();
        List<PShoucang> list = pShoucangService.selectPShoucangList(pShoucang);

        for(int i=0;i<list.size();i++){
            if("ziliao".equals(pShoucang.getSctype())){
                PZiliao pz = ziliaoService.selectPZiliaoById(list.get(i).getBid());
                list.get(i).setZiliao(pz);
            } else if("duwu".equals(pShoucang.getSctype())){
                PDuwu dw = duwuService.selectPDuwuById(list.get(i).getBid());
                list.get(i).setDuwu(dw);
            } else if("lxs".equals(pShoucang.getSctype())){
                PLvxingshe lxs = lvxingsheService.selectPLvxingsheById(list.get(i).getBid());
                list.get(i).setLxs(lxs);
            } else if("chanpin".equals(pShoucang.getSctype())){
                PChanpin chanpin = chanpinService.selectPChanpinById(list.get(i).getBid());
                list.get(i).setChanpin(chanpin);
            }
        }

        return getDataTable(list);
    }

    @GetMapping("/getMyShoucangNum")
    public AjaxResult getMyShoucangNum(PShoucang pShoucang)
    {
        Long num = pShoucangService.count(new QueryWrapper<PShoucang>().eq("uid",SecurityUtils.getUserId()));
        return AjaxResult.success(num);
    }

    @GetMapping("/isshoucang")
    public AjaxResult isshoucang(PShoucang pShoucang)
    {
        pShoucang.setUid(SecurityUtils.getUserId());
        List<PShoucang> list = pShoucangService.selectPShoucangList(pShoucang);
        if(list.size()==0){
            return AjaxResult.success("未收藏",null);
        }else if(list.size()==1){
            return AjaxResult.success(list.get(0));
        }else {
            return AjaxResult.error("获取信息失败");
        }
    }

    @GetMapping("/qxshoucang")
    public AjaxResult qxshoucang(PShoucang pShoucang)
    {
        pShoucang.setUid(SecurityUtils.getUserId());
        return AjaxResult.success(pShoucangService.removeById(pShoucang.getId()));
    }

    @GetMapping("/addshoucang")
    public AjaxResult addshoucang(PShoucang pShoucang)
    {
        pShoucang.setAddTime(DateUtils.getNowDate());
        pShoucang.setUid(SecurityUtils.getUserId());
        return AjaxResult.success(pShoucangService.insertPShoucang(pShoucang));
    }

    /**
     * 导出我的收藏列表
     */
    @PreAuthorize("@ss.hasPermi('ziliao:shoucang:export')")
    @Log(title = "我的收藏", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PShoucang pShoucang)
    {
        List<PShoucang> list = pShoucangService.selectPShoucangList(pShoucang);
        ExcelUtil<PShoucang> util = new ExcelUtil<PShoucang>(PShoucang.class);
        util.exportExcel(response, list, "我的收藏数据");
    }

    /**
     * 获取我的收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('ziliao:shoucang:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pShoucangService.selectPShoucangById(id));
    }

    /**
     * 新增我的收藏
     */
    @PreAuthorize("@ss.hasPermi('ziliao:shoucang:add')")
    @Log(title = "我的收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PShoucang pShoucang)
    {
        return toAjax(pShoucangService.insertPShoucang(pShoucang));
    }

    /**
     * 修改我的收藏
     */
    @PreAuthorize("@ss.hasPermi('ziliao:shoucang:edit')")
    @Log(title = "我的收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PShoucang pShoucang)
    {
        return toAjax(pShoucangService.updatePShoucang(pShoucang));
    }

    /**
     * 删除我的收藏
     */
    @PreAuthorize("@ss.hasPermi('ziliao:shoucang:remove')")
    @Log(title = "我的收藏", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pShoucangService.deletePShoucangByIds(ids));
    }
}
